package com.qch.service;

import com.qch.handler.UserHandler;
import com.qch.json.JSONSerializer;
import com.qch.json.RpcDecoder;
import com.qch.json.RpcEncoder;
import com.qch.json.RpcRequest;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.stereotype.Service;

/**
 * @Author qch
 * @Date 2021/1/27
 */
@Service
public class UserServiceImpl implements UserService {
    public String sayHello(String msg) {
        System.out.println(msg);
        return msg;
    }



    //创建一个启动服务的代码
  /*  public static void startServer(String ip,int port) throws InterruptedException {
        //创建两个线程池对象
        //创建服务端启动引导对象
        //配置启动引导类
        //配置端口
        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(bossGroup,workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        ChannelPipeline pipeline = nioSocketChannel.pipeline();
                        //pipeline.addFirst(new StringEncoder());
                        //pipeline.addLast(new StringDecoder());
                        pipeline.addFirst( new RpcEncoder(RpcRequest.class, new JSONSerializer()));
                        pipeline.addLast( new RpcDecoder(RpcRequest.class, new JSONSerializer()));
                        //把自定义chanelhandler添加到通道中
                        pipeline.addLast(new UserHandler());
                    }
                });
        ChannelFuture sync = serverBootstrap.bind(8999).sync();
    }*/
}
